<?php

// 商品分类

namespace app\console\controller;

use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\Brand as BrandModel;
use app\console\model\GoodsType as GoodsTypeModel;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Db;

class GoodsType extends Controller
{

    protected $beforeActionList = [
        'clearCache' => ['only'],
    ];

    /**
     * 商品分类列表
     * @return mixed
     */
    public function index()
    {
       $data = Cache::get('goodsType');
       $param = Request::instance()->param();
        $where = [];
        $where2 = [];
        $parentStr ='';
       if(!empty($param['title'])) {
           $where['title'] = ['like','%'.$param['title'].'%'];
           $parentStr = Db::name('goods_type')->where($where)->value('parentStr').','.Db::name('goods_type')->where($where)->value('id');
       }
       if(!empty($parentStr))$where2['id'] = ['in',$parentStr];
       $data = GoodsTypeModel::where('is_delete',1)->where($where2)->order('sort','asc')->select()->toArray();

        return $this->fetch('', [
            'mate_title' => '商品分类列表',
            'data'       => ClassifyAlias::dimension($data)
        ]);
    }


    /**
     * 新增产品分类
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param BrandModel $brand
     * @return mixed
     */
    public function create(Request $request, GoodsTypeModel $goodsType, BrandModel $brand)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('GoodsType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $goodsType->allowField(true)->save($data);


            if ($dispose) {
                win('操作成功', Url::build('GoodsType/index'));
            }

            fail('操作失败');

        }

        $data = GoodsTypeModel::where('is_delete',1)->select()->toArray();
        $brand = $brand->field('id,name')->order(['id' => 'desc'])->select();
        return $this->fetch('', [
            "mate_title" => '商品分类添加',
            'data'       => ClassifyAlias::dimension($data),
            'brand'      => $brand

        ]);

    }


    /**
     * 删除商品分类
     * @param $id
     * @param GoodsTypeModel $goodsType
     */
    public function destroy($id, GoodsTypeModel $goodsType)
    {

        if ($goodsType::amputate($id)) {
            win('操作成功', Url::build('GoodsType/index'));
        }

        fail('操作失败');

    }


    /**
     * 多删
     * @param $id
     */
    public function destroyAll($id)
    {

        if (GoodsTypeModel::amputateAll($id)) {
            win('操作成功', Url::build('GoodsType/index'));
        }

        fail('操作失败');

    }


    /**
     * 编辑商品分类
     * @param $id
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param BrandModel $brand
     * @return mixed
     */
    public function update($id, Request $request, GoodsTypeModel $goodsType, BrandModel $brand)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('GoodsType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $goodsType->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('GoodsType/index'));
            }

            fail('操作失败');

        }

        $item = GoodsTypeModel::get(function($e) use ($id){
            $e->where('is_delete',1)
                ->where('id',$id);
        });

        $data = GoodsTypeModel::where('is_delete',1)->order('sort','asc')->select()->toArray();
        return $this->fetch('create', [
            "mate_title" => '编辑商品分类',
            'data'       => ClassifyAlias::dimension($data),
            'item'       => $item,


        ]);
    }


    /**
     * 更改状态
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @return bool|int
     */
    public function auditing(Request $request, GoodsTypeModel $goodsType)
    {
        $id = $request->instance()->param('id');
        $status = $goodsType->where('id', $id)->value('status') == '1' ? 2 : 1;
        if ($goodsType->where('id', $id)->update(['status' => $status])) {
            return $status;
        }
        return false;
    }


    /**
     * 更改推荐状态
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @return bool|int
     */
    public function recommend(Request $request, GoodsTypeModel $goodsType)
    {
        $id = $request->instance()->param('id');

        $recommend = $goodsType->where('id', $id)->value('recommend') == '1' ? 2 : 1;

        if ($goodsType->where('id', $id)->update(['recommend' => $recommend])) {

            return $recommend;
        }

        return false;
    }


    /**
     * 首页推荐
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @return bool|int
     */
    public function index_recommend(Request $request, GoodsTypeModel $goodsType)
    {
        $id = $request->instance()->param('id');

        $recommend = $goodsType->where('id', $id)->value('index_recommend') == '1' ? 2 : 1;

        if ($goodsType->where('id', $id)->update(['index_recommend' => $recommend])) {

            return $recommend;
        }

        return false;
    }


    /**
     * 更改热门市场状态
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @return bool|int
     */
    public function hot_recommend(Request $request, GoodsTypeModel $goodsType)
    {
        $id = $request->instance()->param('id');

        $recommend = $goodsType->where('id', $id)->value('hot_recommend') == '1' ? 2 : 1;

        if ($goodsType->where('id', $id)->update(['hot_recommend' => $recommend])) {

            return $recommend;
        }

        return false;
    }


    public function special_hot_recommend(Request $request, GoodsTypeModel $goodsType)
    {
        $id = $request->instance()->param('id');

        $recommend = $goodsType->where('id', $id)->value('special_hot_recommend') == '1' ? 2 : 1;

        if ($goodsType->where('id', $id)->update(['special_hot_recommend' => $recommend])) {

            return $recommend;
        }

        return false;
    }

    /*
     * 排列顺序
     */
    public function sorting(Request $request, GoodsTypeModel $goodsType)
    {

        $id = $request->instance()->param('orderId/a');
        $sort = $request->instance()->param('sort/a');

        if($id) {
            foreach ($id as $key => $value) {
                $goodsType->where('id', $value)->update(['sort' => $sort[$key]]);
            }
        }

        win('操作成功', Url::build('GoodsType/index'));
    }

    public function code(){
        $param = Request::instance()->param()['code'];

        if(!empty($param)){

            foreach ($param as $k => $v){

                $ret = Db::name('goods_type')->where('id',$k)->setField('code',$v);
            }
            if($ret !== false){
                win('科目代码设置成功',Url::build('index'));
            }
        }

    }




    /*
     * 清除缓存
     */
    protected function clearCache()
    {
        Cache::rm('goodsType');
    }
}
